home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / joe014.zip / JOE014.TAZ / JOE014.tar / blocks.c < prev    next >
C/C++ Source or Header  |  1991-08-27  |  3KB  |  170 lines

  1. /* Various memory block functions
  2.    Copyright (C) 1991 Joseph H. Allen
  3.  
  4. This file is part of JOE (Joe's Own Editor)
  5.  
  6. JOE is free software; you can redistribute it and/or modify it under the terms
  7. of the GNU General Public License as published by the Free Software
  8. Foundation; either version 1, or (at your option) any later version.  
  9.  
  10. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with JOE; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18.  
  19. #include "blocks.h"
  20.  
  21. /* Set block to zero */
  22.  
  23. unsigned char *bzero(bk,sz)
  24. unsigned char *bk;
  25. {
  26. unsigned char *b=bk-1;
  27. if(sz) do b[sz]=0; while(--sz);
  28. return bk;
  29. }
  30.  
  31. /* Set block to unsigned character */
  32.  
  33. unsigned char *bset(bk,sz,c)
  34. unsigned char *bk;
  35. unsigned char c;
  36. {
  37. unsigned char *b=bk-1;
  38. if(sz) do b[sz]=c; while(--sz);
  39. return bk;
  40. }
  41.  
  42. /* Move a possibly overlapping block of memory without loosing any data */
  43.  
  44. unsigned char *bmove(dst,src,sz)
  45. unsigned char *dst;
  46. unsigned char *src;
  47. {
  48. if(src==dst || !sz) return dst;
  49. if(src>dst)
  50.  {
  51.  unsigned x=0;
  52.  do dst[x]=src[x]; while(++x, --sz);
  53.  }
  54. else
  55.  {
  56.  unsigned char *d=dst-1;
  57.  --src;
  58.  do d[sz]=src[sz]; while(--sz);
  59.  } 
  60. return dst;
  61. }
  62.  
  63. /* Move a block in the forward direction */
  64.  
  65. unsigned char *bfwrd(dst,src,sz)
  66. unsigned char *dst;
  67. unsigned char *src;
  68. {
  69. if(src!=dst && sz)
  70.  {
  71.  unsigned x=0;
  72.  do dst[x]=src[x]; while(++x,--sz);
  73.  }
  74. return dst;
  75. }
  76.  
  77. /* Move a block in the backward direction */
  78.  
  79. unsigned char *bbkwd(dst,src,sz)
  80. unsigned char *dst;
  81. unsigned char *src;
  82. {
  83. unsigned char *s=src-1, *d=dst-1;
  84. if(s!=d && sz) do d[sz]=s[sz]; while(--sz);
  85. return dst;
  86. }
  87.  
  88. unsigned umin(a,b)
  89. unsigned a,b;
  90. {
  91. return (a>b)?b:a;
  92. }
  93.  
  94. unsigned umax(a,b)
  95. unsigned a,b;
  96. {
  97. return (a>b)?a:b;
  98. }
  99.  
  100. int min(a,b)
  101. {
  102. return (a>b)?b:a;
  103. }
  104.  
  105. int max(a,b)
  106. {
  107. return (a>b)?a:b;
  108. }
  109.  
  110. /* Compare blocks for equality */
  111.  
  112. int beq(dst,src,sz)
  113. unsigned char *dst;
  114. unsigned char *src;
  115. {
  116. unsigned char *d=dst-1, *s=src-1;
  117. if(!sz) return 1;
  118. do
  119.  if(d[sz]!=s[sz]) return 0;
  120. while(--sz);
  121. return 1;
  122. }
  123.  
  124. /* Compare blocks for equality case insensitive */
  125.  
  126. int bieq(dst,src,sz)
  127. unsigned char *dst;
  128. unsigned char *src;
  129. {
  130. unsigned char *d=dst, *s=src; int cnt=sz;
  131. if(!cnt) return 1;
  132. do
  133.  if(*s>='a' && *s<='z')
  134.   {
  135.   if(*d>='a' && *d<='z') { if(*(d++)!=*(s++)) return 0; }
  136.   else if(*(d++)!=(0x5f&*(s++))) return 0;
  137.   }
  138.  else if(*d>='a' && *d<='z')
  139.   {
  140.   if(*s>='a' && *s<='z') { if(*(d++)!=*(s++)) return 0; }
  141.   else if(*(s++)!=(0x5f&*(d++))) return 0;
  142.   }
  143.  else if(*(d++)!=*(s++)) return 0;
  144. while(--cnt);
  145. return 1;
  146. }
  147.  
  148. unsigned char *bchr(bk,sz,c)
  149. unsigned char *bk;
  150. unsigned char c;
  151. {
  152. unsigned char *s=bk;
  153. int cnt=sz;
  154. if(cnt)
  155.  do if(*s==c) return s;
  156.  while(++s, --cnt);
  157. return 0;
  158. }
  159.  
  160. unsigned char *brchr(bk,sz,c)
  161. unsigned char *bk, c;
  162. {
  163. unsigned char *s=bk+sz;
  164. int cnt=sz;
  165. if(cnt)
  166.  do if(*(--s)==c) return s;
  167.  while(--cnt);
  168. return 0;
  169. }
  170.